package com.peaker.advertisingpro.service

import android.app.AlarmManager
import android.app.IntentService
import android.content.Intent
import android.util.Log
import com.peaker.advertisingpro.receiver.AlarmReceiver
import com.peaker.advertisingpro.utils.AlarmManagerUtil.sendRepeatAlarmBroadcast
import com.peaker.advertisingpro.utils.DateTimeUtil.stringToMillis
import java.util.*


/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/4/18.
 *  代码人生
 */
class AlarmService(name: String = "AlarmService") : IntentService(name) {
    private var alarmDateTime = ""
    override fun onHandleIntent(intent: Intent?) {
        alarmDateTime = intent!!.getStringExtra("alarm_time")
        // taskId用于区分不同的任务
        val taskId = intent.getIntExtra("task_id", 0)

        Log.d(
            "AlarmService", "executed at " + Date().toString()
                .toString() + " @Thread id：" + Thread.currentThread().id
        )

        val alarmDateTimeMillis = stringToMillis(alarmDateTime)
        sendRepeatAlarmBroadcast(
            this, taskId,
            AlarmManager.RTC_WAKEUP, alarmDateTimeMillis, 10 * 1000.toLong(),
            AlarmReceiver::class.java
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("AlarmService", "Alarm Service Destroy");
    }
}